export function timeInterval(timesData) {
	var dateBegin = timesData;
	var dateEnd = new Date();
	var dateDiff = Math.abs( dateEnd.getTime() - dateBegin );
	var yearDiff = Math.floor( dateDiff / (24 * 3600 * 1000 * 365));
	var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));
	var leave1 = dateDiff % (24 * 3600 * 1000);
	var hours = Math.floor(leave1 / (3600 * 1000));
	var leave2 = leavel1 % (3600 * 1000);
	var minutes = Math.floor(leave2 / (60 * 1000));
	var leave3 = leave2 % (60 * 1000);
	var seconds = Math.round(leave3 / 1000);
	var timeString = '';
	if(yearDiff!=0) {
		timeString = yearDiff + "年前";
	} else if(yearDiff == 0 && dayDiff != 0) {
		timeString = dayDiff + "天前";
	} else if(dayDiff == 0 && hours != 0){
		timeString = hours +"小时前"
	}else if(hours == 0 && minutes != 0){
		timeString = minutes +"分钟前"
	}else if(minutes == 0 && seconds != 0){
		timeString = "刚刚"
	}
	return timeString;
}
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}